// @login & register
const express = require('express');
const router = express.Router();
const passport = require('passport');

const Purchase = require('../../models/purchase');

// @route  GET api/supplier/test
// @desc   返回的请求的json数据
// @access public
router.get('/test', (req, res) => {
  res.json({
    msg: 'purchase works'
  });
});

// @route  POST api/purchase/add
// @desc   创建信息接口
// @access Private
router.post(
  '/add',
  passport.authenticate('jwt', {
    session: false
  }),
  (req, res) => {
    const purchaseFields = {};

    if (req.body.operatorName) purchaseFields.operatorName = req.body.operatorName;
    if (req.body.describe) purchaseFields.describe = req.body.describe;
    if (req.body.status) purchaseFields.status = req.body.status;
    if (req.body.type) purchaseFields.type = req.body.type;
    if (req.body.expend) purchaseFields.expend = req.body.expend;
    // if (req.body.remark) profileFields.remark = req.body.remark;

    new Purchase(purchaseFields).save().then(purchase => {
      res.json(purchase);
    });
  }
);

// @route  GET api/purchase
// @desc   获取所有信息
// @access Private

router.get(
  '/',
  passport.authenticate('jwt', {
    session: false
  }),
  (req, res) => {
    Purchase.find().sort({
        date: -1
      })
      .then(purchase => {
        if (!purchase) {
          return res.status(404).json('没有任何内容');
        }

        res.json(purchase);
      })
      .catch(err => res.status(404).json(err));
  }
);



// @route  GET api/profiles/:id
// @desc   获取单个信息
// @access Private

router.get(
  '/:id',
  passport.authenticate('jwt', {
    session: false
  }),
  (req, res) => {
    Profile.findOne({
        _id: req.params.id
      })
      .then(profile => {
        if (!profile) {
          return res.status(404).json('没有任何内容');
        }

        res.json(profile);
      })
      .catch(err => res.status(404).json(err));
  }
);

// @route  POST api/profiles/edit
// @desc   编辑信息接口
// @access Private
router.post(
  '/edit/:id',
  passport.authenticate('jwt', {
    session: false
  }),
  (req, res) => {
    const purchaseFields = {};

    if (req.body.describe) purchaseFields.describe = req.body.describe;
    if (req.body.expend) purchaseFields.expend = req.body.expend;
    if (req.body.operatorName) purchaseFields.operatorName = req.body.operatorName;
    // if (req.body.remark) profileFields.remark = req.body.remark;

    Purchase.findOneAndUpdate({
      _id: req.params.id
    }, {
      $set: purchaseFields
    }, {
      new: true
    }).then(purchase => res.json(purchase));
  }
);

// @route  POST api/profiles/success
// @desc   编辑信息接口
// @access Private
router.post(
  '/success/:id',
  passport.authenticate('jwt', {
    session: false
  }),
  (req, res) => {
    const purchaseFields = {};

    purchaseFields.status = '审核通过';

    Purchase.findOneAndUpdate({
      _id: req.params.id
    }, {
      $set: purchaseFields
    }, {
      new: true
    }).then(purchase => res.json(purchase));
  }
);

// @route  POST api/profiles/success
// @desc   编辑信息接口
// @access Private
router.post(
  '/nopass/:id',
  passport.authenticate('jwt', {
    session: false
  }),
  (req, res) => {
    const purchaseFields = {};

    purchaseFields.status = '审核不通过';

    Purchase.findOneAndUpdate({
      _id: req.params.id
    }, {
      $set: purchaseFields
    }, {
      new: true
    }).then(purchase => res.json(purchase));
  }
);

// @route  POST api/purchase/delete/:id
// @desc   删除信息接口
// @access Private
router.delete(
  '/delete/:id',
  passport.authenticate('jwt', {
    session: false
  }),
  (req, res) => {
    Purchase.findOneAndRemove({
        _id: req.params.id
      })
      .then(purchase => {
        purchase.save().then(purchase => res.json(purchase));
      })
      .catch(err => res.status(404).json('删除失败!'));
  }
);

module.exports = router;